package dao;

import model.User;
import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImp implements UserDao{


    @Override
    public boolean addUser(User user) {
        int result= 0;
        Connection connection=null;
        try {
            connection = DBUtil.getConnection();
            String sql="insert into t_user values(?,?,?,?,?,?)";
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getId());
            preparedStatement.setString(2,user.getName());
            preparedStatement.setString(3,user.getSex());
            preparedStatement.setString(4,user.getBirth());
            preparedStatement.setString(5,user.getEmail());
            preparedStatement.setString(6,user.getContent());
            result = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        if (result>0){
            System.out.println("添加成功");
            return true;
        }else {
            System.out.println("添加失败");
            return false;
        }


    }

    @Override
    public boolean deleteUser(User user) {
        int result= 0;
        Connection connection=null;
        try {
            connection = DBUtil.getConnection();
            String sql="delete from t_user where id=?";
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getId());
            result = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        if (result>0){
            System.out.println("删除成功");
            return  true;
        }else {
            System.out.println("删除失败");
            return false;
        }

    }

    @Override
    public boolean updateUser(User user) {
        int result= 0;
        Connection connection=null;
        try {
            connection = DBUtil.getConnection();
            String sql="update  t_user set name=?,sex=?,birth=?,email=?,content=? where id=?";
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getName());
            preparedStatement.setString(2,user.getSex());
            preparedStatement.setString(3,user.getBirth());
            preparedStatement.setString(4,user.getEmail());
            preparedStatement.setString(5,user.getContent());
            preparedStatement.setString(6,user.getId());
            result = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        if (result>0){
            System.out.println("修改成功");
            return true;
        }else {
            System.out.println("修改失败");
            return false;
        }

    }

    @Override
    public User queryUser(User user) {
        Connection connection=null;
        ResultSet resultSet=null;
        try {
            connection = DBUtil.getConnection();
            String sql="select *  from t_user where id=?";
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getId());
            resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                User user1=new User(resultSet.getString("id"),resultSet.getString("name"),resultSet.getString("sex"),resultSet.getString("birth"),resultSet.getString("email"),resultSet.getString("content"));
                return user1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;

    }

    @Override
    public List<User> queryAllUser() {
        Connection connection=null;
        ResultSet resultSet=null;
        try {
            connection = DBUtil.getConnection();
            String sql="select *  from t_user";
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            resultSet=preparedStatement.executeQuery();
            ArrayList<User> users = new ArrayList<>();
            while (resultSet.next()){
                String id = resultSet.getString("id");
                String name = resultSet.getString("name");
                String sex = resultSet.getString("sex");
                String birth=resultSet.getString("birth");
                String email = resultSet.getString("email");
                String content = resultSet.getString("content");
                User user=new User(id,name,sex,birth,email,content);
                users.add(user);
            }
            return users;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
